package com.ele.dao.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import com.ele.dao.ShopCategoryDao;
import com.ele.pojo.ShopCategory;
import com.ele.utils.JDBCUtils;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.List;

public class ShopCategoryDaoImpl implements ShopCategoryDao {
    private DataSource dataSource = JDBCUtils.getDatasource();
    public List<ShopCategory> shopCategoryList() throws SQLException {
        return Db.use(dataSource).query("select * from shop_category",ShopCategory.class);
    }

    @Override
    public List<ShopCategory> findShopCategory(String currentPage, String pageSize) throws SQLException {
        Integer page = 1;
        Integer size = 5;
        if(StrUtil.isNotBlank(currentPage)){
            page = Integer.valueOf(currentPage);
        }
        if(StrUtil.isNotBlank(pageSize)){
            size = Integer.parseInt(pageSize);
        }
        page = (page -1) * size;
        return Db.use(dataSource).query("select * from shop_category  limit ? , ? " ,
                ShopCategory.class , page, size );
    }

    @Override
    public Long selectCount() throws SQLException {
        return Db.use(dataSource).count(Entity.create("shop_category"));
    }

    @Override
    public Integer shopCategoryDelete(String categoryId) throws SQLException {
        return Db.use(dataSource).del("shop_category","shop_category_id",categoryId);
    }

    @Override
    public Integer shopCategoryUpdate(String categoryName, String shopCategoryId) throws SQLException {
        return Db.use(dataSource).update(
                Entity.create().set("category_name",categoryName) ,
                Entity.create("shop_category").set("shop_category_id",shopCategoryId));
    }

    @Override
    public Integer shopCategoryAdd(String categoryName) throws SQLException {
        return Db.use(dataSource).insert(
                Entity.create("shop_category")
                        .set("category_name",categoryName));
    }
}
